package cpsobras.web;

import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import cpsobras.cargo.Cargo;
import cpsobras.cargo.CargoRN;

@ManagedBean(name="cargoBean")
@SessionScoped
public class CargoBean implements Serializable {

	private static final long serialVersionUID = 1212893299609063624L;
	
	private Cargo cargo = new Cargo();
	private List<Cargo> lista;
	
	public String novo(){
		this.cargo = new Cargo();
		return "cargo";
	}

	public String salvar(){
		CargoRN cargoRN = new CargoRN();
		cargoRN.salvar(this.cargo);
		FacesContext context = FacesContext.getCurrentInstance();
		FacesMessage facesMessage = new FacesMessage("Cadastrado/Alterado com sucesso!");
		context.addMessage(null, facesMessage);
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("cargoBean");
		this.cargo = null;
		return "listar_cargo";
	}
	
	public String excluir(){
		CargoRN cargoRN = new CargoRN();
		cargoRN.excluir(this.cargo);
		this.lista = null;
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("cargoBean");
		return "listar_cargo";
	}
	
	public String alterar(){
		return "cargo";
	}
	
	public String voltar(){
		return "listar_cargo";
	}

	public List<Cargo> getLista() {
		if(this.lista == null){
			CargoRN cargoRN = new CargoRN();
			this.lista = cargoRN.listar();
		}
		return this.lista;
	}

	public Cargo getCargo() {
		return cargo;
	}

	public void setCargo(Cargo cargo) {
		this.cargo = cargo;
	}

	public void setLista(List<Cargo> lista) {
		this.lista = lista;
	}
	
}
